package com.future.controller;

import com.future.domain.DTO.UserDTO;
import com.future.service.FollowService;
import com.future.domain.Result;
import com.future.allUtils.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 关注相关接口
 */
@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    private FollowService followService;

    /**
     * 关注用户
     */
    @ApiOperation("关注用户")
    @PostMapping
    public Result followUser(@RequestParam Long followedUserId) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getCurrentUserId();
        followService.followUser(userId, followedUserId);
        return Result.ok(true);
    }

    /**
     * 取消关注
     */
    @ApiOperation("取消关注")
    @DeleteMapping
    public Result unfollowUser(@RequestParam Long followedUserId) {
        Long userId = SecurityUtils.getCurrentUserId();
        followService.unfollowUser(userId, followedUserId);
        return Result.ok(true);
    }

    /**
     * 检查是否已关注
     */
    @ApiOperation("检查是否已关注")
    @GetMapping("/check")
    public Result isFollowing(@RequestParam Long followedUserId) {
        Long userId = SecurityUtils.getCurrentUserId();
        boolean isFollowing = followService.isFollowing(userId, followedUserId);
        return Result.ok(isFollowing);
    }

    /**
     * 获取用户的关注列表
     */
    @ApiOperation("获取关注列表")
    @GetMapping("/following")
    public Result getFollowingList(@RequestParam(defaultValue = "1")Integer pageNum
                                ,  @RequestParam(defaultValue = "10")Integer pageSize) {
        Long userId = SecurityUtils.getCurrentUserId();
        List<UserDTO> followingList = followService.getFollowingList(userId, pageNum, pageSize);
        return Result.ok(followingList);
    }

    /**
     * 获取用户的粉丝列表
     */
    @ApiOperation("获取粉丝列表")
    @GetMapping("/followers")
    public Result getFollowerList(@RequestParam(defaultValue = "1")Integer pageNum
            ,  @RequestParam(defaultValue = "10")Integer pageSize) {
        Long userId = SecurityUtils.getCurrentUserId();
        List<UserDTO> followerList = followService.getFollowerList(userId, pageNum, pageSize);
        return Result.ok(followerList);
    }

    /**
     * 获取用户的关注数量
     */
    @ApiOperation("获取关注数量")
    @GetMapping("/count/following")
    public Result getFollowingCount() {
        Long userId = SecurityUtils.getCurrentUserId();
        Long count = followService.getFollowingCount(userId);
        return Result.ok(count);
    }

    /**
     * 获取用户的粉丝数量
     */
    @ApiOperation("获取粉丝数量")
    @GetMapping("/count/followers")
    public Result getFollowerCount() {
        Long userId = SecurityUtils.getCurrentUserId();
        Long count = followService.getFollowerCount(userId);
        return Result.ok(count);
    }
}